var details = {};
var goodsId; //商品ID
var shopId;
var time;
var dateLong;
var peopleId; //服务人员ID
var num; //商品数量
var one; //选中的商家优惠券
$(function() {
	//获取url中的参数
	var loc = window.location.search.substr(1);
	var arr = []

	loc = loc.split('&');
	for(var i = 0; i < loc.length; i++) {
		var theRequest = new Object();
		theRequest.title = loc[i].split("=")[0];
		theRequest.value = loc[i].split("=")[1];

		arr.push(theRequest)
	}

	goodsId = arr[4].value;
	shopId = arr[3].value;
	time = arr[1].value;
	dateLong = arr[0].value;
	peopleId = arr[2].value;
	num = arr[5].value;

	var to = new Date(parseInt(dateLong)).toLocaleString().replace(/:\d{1,2}$/, ' ');
	to = to.split(' ');

	details.shuliang = num;
	details.servTime = to[0] + ' ' + time;
	console.log(JSON.stringify(details))

	getGoods();
	getStaff();
	getyhq();
	count();
	getData();
	gotaddress();
	getAddress();
	getMoney();

})
//获取要支付金额
function getMoney(){
	$.ajax({
		type:"get",
		url:baseUrl+"hsent-xxyl-user/orderDetail/calculateMoney",
		async:false,
		data:{
			goodsId:goodsId,
			buyCount:num,
			ptCoupons:details.num,//平台抵扣券数量
			userCouponsDiscountId:''
		},
		success:function(res){
			console.log(res.data);
			details.payMoney = res.data.realPay;
			getData();

		}
	});

}

//提交订单
function submit() {
	console.log(details);
	var txt = document.querySelector('textarea').value;
	var loca = details.address.province + details.address.city + details.address.area + details.address.address;
	$.ajax({
		type: "get",
		url: baseUrl + "hsent-xxyl-user/orderDetail/addOrderDetailService",
		async: false,
		data: {
			shopId: shopId, //商家ID
			goodsId: goodsId, //产品服务ID
			buyCount: num, //产品服务购买数量
			userCouponsDiscountId: details.yhqID, //商家优惠券抵扣编号  one
			ptCouponsDiscount: details.num * 10, //平台消费抵扣券抵扣金额 two
			orderAmount: details.shuliang * details.goods.hsPrice, //订单金额 three
			payAmount: details.payMoney, //实付金额  four
			contactName: details.address.receiver, //预约联系人名称  five
			contactPhone: details.address.telphone, //预约联系人电话 six
			applyServiceDate: dateLong, //预约服务日期时间戳
			applyServiceTime: time, //预约服务时间段  格式:8:00-10:100
			applyServiceStaffsIds: peopleId, //预约服务人员集合字符串  例如:12,13,15
			applyServiceAddress: loca, //预约上门服务地址 seven
			userNotes: txt, //用户下单备注
			exveptShopTime: dateLong,
			pc:1
		},
		success: function(res) {
			console.log('跳转支付')
			$('#cover1').remove()
			$('#payBody').html(res);
		}
	})
}


//获取项目
function getGoods() {
	$.ajax({
		type: "get",
		url: baseUrl + "hsent-xxyl-user/service/tGoodsUserBasicInfo",
		async: false,
		data: {
			id: goodsId,
			shopId: shopId
		},
		success: function(res) {
			var list = res.data.choiceDayVoList;

			details.goods = res.data;

			return details;
		}
	});
	return details;
}
//获取服务人员信息
function getStaff() {
	$.ajax({
		type: "get",
		url: baseUrl + "hsent-xxyl-user/staffs/selectStaffs",
		async: false,
		data: {
			staffId: peopleId
		},
		success: function(res) {

			details.staff = res.data;
			return details;
		}
	});
	return details;
}
//优惠券
function getyhq() {
	$.ajax({
		type: "get",
		url: baseUrl + "hsent-xxyl-user/coupon/getCouponGot",
		async: false,
		data: {
			shopId: shopId,
			goodsId: goodsId,
			count: num
		},
		success: function(res) {

			details.yhq = res.data.available;
			return details;
		}
	});
	return details;
}

//选中优惠券
function selectedYhq(list) {
	console.log(list);
	details.selected = list.couponAmount;
	details.yhqID = list.id;
    getMoney()
}

function count() {
	$.ajax({
		type: "get",
		url: baseUrl + "hsent-xxyl-user/consumer/getCanUseVoucherNum",
		async: false,
		data: {

			goodsId: goodsId,
			count: num
		},
		success: function(res) {

			details.num = res.data.num;
			return details;
		}
	})
	return details;
}
//增加优惠券
function addNum() {
	if(details.num < 5) {
		details.num++;
		getMoney()

	}
	return;

}
//减少优惠券
function minusNum() {
	console.log(details.num);
	if(details.num > 0) {
		details.num--;
		 getMoney()
	}
	return;
}

//模板渲染
function getData() {
	orderCon = template('test', details);
	document.getElementById('goods').innerHTML = orderCon;
}

var address = {};
var addCon;
//地址模态框
function gotaddress() {
	$.ajax({
		type: "get",
		url: baseUrl + "hsent-xxyl-user/consumer/selectConsumerAddrs",
		async: false,
		success: function(res) {

			address.list = res.data;
		}
	});
}
//生成模态框地址模板
function getAddress() {
	addCon = template('addAddress', address);
	document.getElementById('myModal').innerHTML = addCon;
}

//单选按钮
//选择地址

function getRadio(list) {

	$('#myModal').on('click', '.queren', function() {

		details.address = list;

		getData();

		$('.modal-footer .queren').attr('data-dismiss', 'modal');

	})

}

//新增服务地址
function newAddress() {
	var userName = $('.myModalEdit form .username').val(); //联系人
	var pro = $('.myModalEdit form .details_1').val(); //省
	var city = $('.myModalEdit form .details_2').val(); //市
	var county = $('.myModalEdit form .details_3').val(); // 区
	var detLoc = $('.myModalEdit form .details_4').val(); //具体地址
	var tel_top = $('.myModalEdit form .tel_top').val();
	var tel_body = $('.myModalEdit form .tel_body').val();
	var pro1 = $("#selectProvince_query option:selected").text();
	var city1 = $("#selectCity_query option:selected").text();
	var county1 = $("#selectCounty_query option:selected").text()
	console.log(pro1, city1, county1);

	$.ajax({
		type: "get",
		url: baseUrl + "hsent-xxyl-user/consumer/addConsumerAddr",
		async: true,
		data: {
			receiver: userName, // 收货人
			province: pro1, //省份名称
			city: city1, //城市名称
			area: county1, //区域名称
			provinceNo: pro, //所在省编号
			cityNo: city, //所在城市编号
			areaNo: county, // 区域名称编号
			address: detLoc, // 地址名称
			postCode: '', //邮编
			num: tel_body,
			isDefault: 0

		},
		success: function(res) {
			gotaddress();
			getAddress();
			console.log(res);
			$('.myModalEdit .modal-footer .add').attr('data-dismiss', 'modal');
		}
	});

}
//删除地址
function delAddress(list) {
	console.log(list);
	$.ajax({
		type: "get",
		url: baseUrl + "hsent-xxyl-user/consumer/delConsumerAddr",
		async: true,
		data: {
			addrNo: list.addrNo
		},
		success: function(res) {
			gotaddress();
			getAddress();
			console.log(res);
		}
	});
}
//设为默认
function setDefault(list) {
	console.log(list);
	$.ajax({
		type: 'get',
		url: baseUrl + 'hsent-xxyl-user/consumer/setConsumerAddrToDefault',
		data: {
			addrNo: list.addrNo
		},
		success: function(res) {
			gotaddress();
			getAddress();
			console.log(res);
		}
	})
}
//修改地址弹出框
function edit(list){
	console.log(list);
	  $('#ModalEdit form .username').val(list.receiver); //联系人

      $('#ModalEdit form .details_4').val(list.address); //具体地址

      $('#ModalEdit form .tel_body').val(list.telphone);
      $("#ModalEdit #selectProvince_query option:selected").text(list.province);
      $("#ModalEdit #selectCity_query option:selected").text(list.city);
      $("#ModalEdit #selectCounty_query option:selected").text(list.area)
	  $("#ModalEdit  .modal-footer .add").attr('title',list.addrNo);

}

//新增服务地址
function submitEdit() {
	var userName = $('#ModalEdit form .username').val(); //联系人
	var pro = $('#ModalEdit form .details_1').val(); //省
	var city = $('#ModalEdit form .details_2').val(); //市
	var county = $('#ModalEdit form .details_3').val(); // 区
	var detLoc = $('#ModalEdit form .details_4').val(); //具体地址
	var tel_top = $('#ModalEdit form .tel_top').val();
	var tel_body = $('#ModalEdit form .tel_body').val();
	var pro1 = $("#ModalEdit #selectProvince_query option:selected").text();
	var city1 = $("#ModalEdit #selectCity_query option:selected").text();
	var county1 = $("#ModalEdit #selectCounty_query option:selected").text()
	var addrNo =  $("#ModalEdit .modal-footer .add").prop('title');
	 console.log(addrNo);

	$.ajax({
		type: "get",
		url: baseUrl + "hsent-xxyl-user/consumer/updateConsumerAddr",
		async: true,
		data: {
			addrNo:addrNo,
			receiver: userName, // 收货人
			province: pro1, //省份名称
			city: city1, //城市名称
			area: county1, //区域名称
			provinceNo: pro, //所在省编号
			cityNo: city, //所在城市编号
			areaNo: county, // 区域名称编号
			address: detLoc, // 地址名称
			postCode: '', //邮编
			num: tel_body,
			isDefault: 0

		},
		success: function(res) {
			gotaddress();
			getAddress();
			console.log(res);
			$('#ModalEdit .modal-footer .add').attr('data-dismiss', 'modal');
		}
	});

}


